<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="###Java 线程池(Executor)  ThreadPoolExecutor使用1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950/** * ThreadPoolExecutor 使用测试类 * 在java doc中，并不提倡我们直接使用ThreadPoolEx">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Java-线程池(Executor)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Java/Java-线程池(Executor)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="###Java 线程池(Executor)  ThreadPoolExecutor使用1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950/** * ThreadPoolExecutor 使用测试类 * 在java doc中，并不提倡我们直接使用ThreadPoolEx">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-08-06T10:50:28.634Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java-线程池(Executor)">
<meta name="twitter:description" content="###Java 线程池(Executor)  ThreadPoolExecutor使用1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950/** * ThreadPoolExecutor 使用测试类 * 在java doc中，并不提倡我们直接使用ThreadPoolEx">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Java/Java-线程池(Executor)/"/>





  <title>Java-线程池(Executor) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Java/Java-线程池(Executor)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Java-线程池(Executor)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:39+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>###Java 线程池(Executor)</p>
<ol>
<li>ThreadPoolExecutor使用<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * ThreadPoolExecutor 使用测试类</span><br><span class="line"> * 在java doc中，并不提倡我们直接使用ThreadPoolExecutor，</span><br><span class="line"> * 而是使用Executors类中提供的几个静态方法来创建线程池</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class ThreadPoolExecutorTest &#123;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 当前类主方法入口，在主函数中静态调用即可</span><br><span class="line">	 */</span><br><span class="line">	public static void exec() &#123;</span><br><span class="line">		// 实例化线程池</span><br><span class="line">		// corePoolSize : 核心池的大小（即线程池中的线程数目大于这个参数时，提交的任务会被放进任务缓存队列）</span><br><span class="line">		// maximumPoolSize : 线程池最大能容忍的线程数</span><br><span class="line">		// keepAliveTime : 线程存活时间</span><br><span class="line">		// unit : 参数keepAliveTime的时间单位</span><br><span class="line">		// workQueue : 任务缓存队列，用来存放等待执行的任务</span><br><span class="line">		ThreadPoolExecutor executor = new ThreadPoolExecutor(</span><br><span class="line">				5, 10, 200, TimeUnit.MILLISECONDS, </span><br><span class="line">				new ArrayBlockingQueue&lt;Runnable&gt;(5));</span><br><span class="line">		for (int i = 0; i &lt; 15; i++) &#123;</span><br><span class="line">			MyTask task = new MyTask(i);</span><br><span class="line">			executor.execute(task); // 执行任务</span><br><span class="line">			System.out.println(&quot;线程池中线程数目：&quot;+executor.getPoolSize()+&quot;，队列中等待执行的任务数目：&quot;+</span><br><span class="line">		             executor.getQueue().size()+&quot;，已执行玩别的任务数目：&quot;+executor.getCompletedTaskCount());</span><br><span class="line">		&#125;</span><br><span class="line">		// 不会立即终止线程池，而是要等所有任务缓存队列中的任务都执行完后才终止，但再也不会接受新的任务</span><br><span class="line">		executor.shutdown();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	static class MyTask implements Runnable&#123;</span><br><span class="line">		private int taskNum;</span><br><span class="line">		</span><br><span class="line">		public MyTask(int num) &#123;</span><br><span class="line">			this.taskNum = num;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		public void run() &#123;</span><br><span class="line">			System.out.println(&quot;正在执行task: &quot; + taskNum);</span><br><span class="line">			try &#123;</span><br><span class="line">				// 当前线程睡眠4秒</span><br><span class="line">				Thread.currentThread().sleep(4000);</span><br><span class="line">			&#125; catch (InterruptedException e) &#123;&#125;</span><br><span class="line">			System.out.println(&quot;task: &quot; + taskNum + &quot;执行完毕&quot;);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<a id="more"></a>
<p>执行结果:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">正在执行task: 0</span><br><span class="line">线程池中线程数目：1，队列中等待执行的任务数目：0，已执行玩别的任务数目：0</span><br><span class="line">线程池中线程数目：2，队列中等待执行的任务数目：0，已执行玩别的任务数目：0</span><br><span class="line">正在执行task: 1</span><br><span class="line">正在执行task: 2</span><br><span class="line">线程池中线程数目：3，队列中等待执行的任务数目：0，已执行玩别的任务数目：0</span><br><span class="line">线程池中线程数目：4，队列中等待执行的任务数目：0，已执行玩别的任务数目：0</span><br><span class="line">正在执行task: 3</span><br><span class="line">线程池中线程数目：5，队列中等待执行的任务数目：0，已执行玩别的任务数目：0</span><br><span class="line">线程池中线程数目：5，队列中等待执行的任务数目：1，已执行玩别的任务数目：0</span><br><span class="line">线程池中线程数目：5，队列中等待执行的任务数目：2，已执行玩别的任务数目：0</span><br><span class="line">线程池中线程数目：5，队列中等待执行的任务数目：3，已执行玩别的任务数目：0</span><br><span class="line">线程池中线程数目：5，队列中等待执行的任务数目：4，已执行玩别的任务数目：0</span><br><span class="line">线程池中线程数目：5，队列中等待执行的任务数目：5，已执行玩别的任务数目：0</span><br><span class="line">正在执行task: 4</span><br><span class="line">线程池中线程数目：6，队列中等待执行的任务数目：5，已执行玩别的任务数目：0</span><br><span class="line">正在执行task: 10</span><br><span class="line">线程池中线程数目：7，队列中等待执行的任务数目：5，已执行玩别的任务数目：0</span><br><span class="line">正在执行task: 11</span><br><span class="line">线程池中线程数目：8，队列中等待执行的任务数目：5，已执行玩别的任务数目：0</span><br><span class="line">正在执行task: 12</span><br><span class="line">线程池中线程数目：9，队列中等待执行的任务数目：5，已执行玩别的任务数目：0</span><br><span class="line">正在执行task: 13</span><br><span class="line">线程池中线程数目：10，队列中等待执行的任务数目：5，已执行玩别的任务数目：0</span><br><span class="line">正在执行task: 14</span><br><span class="line">task: 1执行完毕</span><br><span class="line">task: 0执行完毕</span><br><span class="line">task: 2执行完毕</span><br><span class="line">正在执行task: 5</span><br><span class="line">正在执行task: 6</span><br><span class="line">task: 4执行完毕</span><br><span class="line">正在执行task: 8</span><br><span class="line">task: 3执行完毕</span><br><span class="line">正在执行task: 9</span><br><span class="line">task: 12执行完毕</span><br><span class="line">task: 13执行完毕</span><br><span class="line">正在执行task: 7</span><br><span class="line">task: 10执行完毕</span><br><span class="line">task: 11执行完毕</span><br><span class="line">task: 14执行完毕</span><br><span class="line">task: 5执行完毕</span><br><span class="line">task: 9执行完毕</span><br><span class="line">task: 6执行完毕</span><br><span class="line">task: 8执行完毕</span><br><span class="line">task: 7执行完毕</span><br></pre></td></tr></table></figure></p>
<ol start="2">
<li><p>Executors使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 线程池ExecutorService测试类</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class ExecutorsTest &#123;</span><br><span class="line"></span><br><span class="line">	public static void exec() &#123;</span><br><span class="line">		/**</span><br><span class="line">		 * CachedThreadPool会创建一个缓存区，将初始化的线程缓存起来。会终止并且从缓存中移除已有60秒未被使用的线程。</span><br><span class="line">		 *	如果线程有可用的，就使用之前创建好的线程，</span><br><span class="line">		 *  如果线程没有可用的，就新创建线程。</span><br><span class="line">		 *		</span><br><span class="line">		 *	重用：缓存型池子，先查看池中有没有以前建立的线程，如果有，就reuse；如果没有，就建一个新的线程加入池中</span><br><span class="line">		 * 	使用场景：缓存型池子通常用于执行一些生存期很短的异步型任务，因此在一些面向连接的daemon型SERVER中用得不多。</span><br><span class="line">		 *	超时：能reuse的线程，必须是timeout IDLE内的池中线程，缺省timeout是60s，超过这个IDLE时长，线程实例将被终止及移出池。</span><br><span class="line">		 *	结束：注意，放入CachedThreadPool的线程不必担心其结束，超过TIMEOUT不活动，其会自动被终止。</span><br><span class="line">		 */</span><br><span class="line">		ExecutorService cachedThreadPool = Executors.newCachedThreadPool();</span><br><span class="line">		</span><br><span class="line">		/**</span><br><span class="line">		 * 在FixedThreadPool中，有一个固定大小的池。</span><br><span class="line">		 *	如果当前需要执行的任务超过池大小，那么多出的任务处于等待状态，直到有空闲下来的线程执行任务，</span><br><span class="line">		 *	如果当前需要执行的任务小于池大小，空闲的线程也不会去销毁。</span><br><span class="line">		 *	</span><br><span class="line">		 *	重用：fixedThreadPool与cacheThreadPool差不多，也是能reuse就用，但不能随时建新的线程</span><br><span class="line">		 *	固定数目：其独特之处在于，任意时间点，最多只能有固定数目的活动线程存在，此时如果有新的线程要建立，只能放在另外的队列中等待，直到当前的线程中某个线程终止直接被移出池子</span><br><span class="line">		 *	超时：和cacheThreadPool不同，FixedThreadPool没有IDLE机制（可能也有，但既然文档没提，肯定非常长，类似依赖上层的TCP或UDP IDLE机制之类的），</span><br><span class="line">		 *	使用场景：所以FixedThreadPool多数针对一些很稳定很固定的正规并发线程，多用于服务器</span><br><span class="line">		 *	源码分析：从方法的源代码看，cache池和fixed 池调用的是同一个底层池，只不过参数不同：</span><br><span class="line">		 *	fixed池线程数固定，并且是0秒IDLE（无IDLE）</span><br><span class="line">		 *	cache池线程数支持0-Integer.MAX_VALUE(显然完全没考虑主机的资源承受能力），60秒IDLE</span><br><span class="line">		 */</span><br><span class="line">		ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);</span><br><span class="line">		</span><br><span class="line">		/**</span><br><span class="line">		 * ScheduledThreadPool是一个固定大小的线程池，与FixedThreadPool类似，执行的任务是定时执行。</span><br><span class="line">		 */</span><br><span class="line">		ExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);</span><br><span class="line">		</span><br><span class="line">		/**</span><br><span class="line">		 * SingleThreadExecutor得到的是一个单个的线程，这个线程会保证你的任务执行完成。</span><br><span class="line">		 * 如果当前线程意外终止，会创建一个新线程继续执行任务，这和我们直接创建线程不同，也和newFixedThreadPool(1)不同。</span><br><span class="line">		 */</span><br><span class="line">		ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();</span><br><span class="line">		</span><br><span class="line">		/**</span><br><span class="line">		 * 创建一个单线程执行器，它可以调度命令在给定的延迟之后运行，或者定期执行。(注意，如果此单线程由于在关闭前执行失败而终止，</span><br><span class="line">		 * 则新线程将在需要执行后续任务时取代其位置。)任务可以保证按顺序执行，在任何给定的时间内，不超过一个任务是活动的。</span><br><span class="line">		 * 与其他等效的&#123; @ code newScheduledThreadPool(1)&#125;不同的是，返回的executor不能被重新配置以使用额外的线程。</span><br><span class="line">		 */</span><br><span class="line">		ExecutorService singleThreadScheduledExecutor = Executors.newSingleThreadScheduledExecutor();</span><br><span class="line">		</span><br><span class="line">		/**</span><br><span class="line">		 * 创建一个系统所能容纳的线程池大小(Runtime.getRuntime().availableProcessors())</span><br><span class="line">		 * 作为并行级别</span><br><span class="line">		 */</span><br><span class="line">		ExecutorService workStealingPool = Executors.newWorkStealingPool();</span><br><span class="line">		</span><br><span class="line">		run(cachedThreadPool);</span><br><span class="line">//		run(fixedThreadPool);</span><br><span class="line">//		run(scheduledThreadPool);</span><br><span class="line">//		run(singleThreadExecutor);</span><br><span class="line">//		run(singleThreadScheduledExecutor);</span><br><span class="line">//		run(workStealingPool);</span><br><span class="line">	&#125;	</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 执行任务</span><br><span class="line">	 * @param cachedThreadPool 线程池服务</span><br><span class="line">	 */</span><br><span class="line">	private static void run(ExecutorService threadPool) &#123;</span><br><span class="line">		for (int i = 0; i &lt; 5; i++) &#123;</span><br><span class="line">			final int taskID = i;</span><br><span class="line">			threadPool.execute(new Runnable() &#123;</span><br><span class="line">				</span><br><span class="line">				public void run() &#123;</span><br><span class="line">					for (int j = 0; j &lt; 5; j++) &#123;</span><br><span class="line">						try &#123;</span><br><span class="line">							// 为了测试出效果，让每次任务执行都需要一定时间   </span><br><span class="line">							Thread.currentThread().sleep(20);</span><br><span class="line">						&#125; catch (InterruptedException e) &#123;</span><br><span class="line">							e.printStackTrace();</span><br><span class="line">						&#125;</span><br><span class="line">						System.out.println(&quot;第&quot; + taskID + &quot;次任务的第&quot; + j + &quot;次执行&quot;);</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;);</span><br><span class="line">		&#125;</span><br><span class="line">		// 不会立即终止线程池，而是要等所有任务缓存队列中的任务都执行完后才终止，但再也不会接受新的任务</span><br><span class="line">		threadPool.shutdown();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>run(cachedThreadPool)执行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">第4次任务的第0次执行</span><br><span class="line">第1次任务的第0次执行</span><br><span class="line">第0次任务的第0次执行</span><br><span class="line">第3次任务的第0次执行</span><br><span class="line">第2次任务的第0次执行</span><br><span class="line">第0次任务的第1次执行</span><br><span class="line">第4次任务的第1次执行</span><br><span class="line">第2次任务的第1次执行</span><br><span class="line">第3次任务的第1次执行</span><br><span class="line">第1次任务的第1次执行</span><br><span class="line">第1次任务的第2次执行</span><br><span class="line">第0次任务的第2次执行</span><br><span class="line">第2次任务的第2次执行</span><br><span class="line">第4次任务的第2次执行</span><br><span class="line">第3次任务的第2次执行</span><br><span class="line">第1次任务的第3次执行</span><br><span class="line">第4次任务的第3次执行</span><br><span class="line">第0次任务的第3次执行</span><br><span class="line">第3次任务的第3次执行</span><br><span class="line">第2次任务的第3次执行</span><br><span class="line">第1次任务的第4次执行</span><br><span class="line">第4次任务的第4次执行</span><br><span class="line">第2次任务的第4次执行</span><br><span class="line">第0次任务的第4次执行</span><br><span class="line">第3次任务的第4次执行</span><br></pre></td></tr></table></figure>
<p>run(fixedThreadPool)执行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">第2次任务的第0次执行</span><br><span class="line">第1次任务的第0次执行</span><br><span class="line">第0次任务的第0次执行</span><br><span class="line">第2次任务的第1次执行</span><br><span class="line">第1次任务的第1次执行</span><br><span class="line">第0次任务的第1次执行</span><br><span class="line">第1次任务的第2次执行</span><br><span class="line">第0次任务的第2次执行</span><br><span class="line">第2次任务的第2次执行</span><br><span class="line">第1次任务的第3次执行</span><br><span class="line">第0次任务的第3次执行</span><br><span class="line">第2次任务的第3次执行</span><br><span class="line">第1次任务的第4次执行</span><br><span class="line">第0次任务的第4次执行</span><br><span class="line">第2次任务的第4次执行</span><br><span class="line">第3次任务的第0次执行</span><br><span class="line">第4次任务的第0次执行</span><br><span class="line">第3次任务的第1次执行</span><br><span class="line">第4次任务的第1次执行</span><br><span class="line">第3次任务的第2次执行</span><br><span class="line">第4次任务的第2次执行</span><br><span class="line">第4次任务的第3次执行</span><br><span class="line">第3次任务的第3次执行</span><br><span class="line">第3次任务的第4次执行</span><br><span class="line">第4次任务的第4次执行</span><br></pre></td></tr></table></figure>
<p>run(scheduledThreadPool)执行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">第2次任务的第0次执行</span><br><span class="line">第1次任务的第0次执行</span><br><span class="line">第0次任务的第0次执行</span><br><span class="line">第0次任务的第1次执行</span><br><span class="line">第2次任务的第1次执行</span><br><span class="line">第1次任务的第1次执行</span><br><span class="line">第1次任务的第2次执行</span><br><span class="line">第2次任务的第2次执行</span><br><span class="line">第0次任务的第2次执行</span><br><span class="line">第2次任务的第3次执行</span><br><span class="line">第0次任务的第3次执行</span><br><span class="line">第1次任务的第3次执行</span><br><span class="line">第2次任务的第4次执行</span><br><span class="line">第1次任务的第4次执行</span><br><span class="line">第0次任务的第4次执行</span><br><span class="line">第3次任务的第0次执行</span><br><span class="line">第4次任务的第0次执行</span><br><span class="line">第3次任务的第1次执行</span><br><span class="line">第4次任务的第1次执行</span><br><span class="line">第3次任务的第2次执行</span><br><span class="line">第4次任务的第2次执行</span><br><span class="line">第3次任务的第3次执行</span><br><span class="line">第4次任务的第3次执行</span><br><span class="line">第3次任务的第4次执行</span><br><span class="line">第4次任务的第4次执行</span><br></pre></td></tr></table></figure>
<p>run(singleThreadExecutor)执行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">第0次任务的第0次执行</span><br><span class="line">第0次任务的第1次执行</span><br><span class="line">第0次任务的第2次执行</span><br><span class="line">第0次任务的第3次执行</span><br><span class="line">第0次任务的第4次执行</span><br><span class="line">第1次任务的第0次执行</span><br><span class="line">第1次任务的第1次执行</span><br><span class="line">第1次任务的第2次执行</span><br><span class="line">第1次任务的第3次执行</span><br><span class="line">第1次任务的第4次执行</span><br><span class="line">第2次任务的第0次执行</span><br><span class="line">第2次任务的第1次执行</span><br><span class="line">第2次任务的第2次执行</span><br><span class="line">第2次任务的第3次执行</span><br><span class="line">第2次任务的第4次执行</span><br><span class="line">第3次任务的第0次执行</span><br><span class="line">第3次任务的第1次执行</span><br><span class="line">第3次任务的第2次执行</span><br><span class="line">第3次任务的第3次执行</span><br><span class="line">第3次任务的第4次执行</span><br><span class="line">第4次任务的第0次执行</span><br><span class="line">第4次任务的第1次执行</span><br><span class="line">第4次任务的第2次执行</span><br><span class="line">第4次任务的第3次执行</span><br><span class="line">第4次任务的第4次执行</span><br></pre></td></tr></table></figure>
<p>run(singleThreadScheduledExecutor)执行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">第0次任务的第0次执行</span><br><span class="line">第0次任务的第1次执行</span><br><span class="line">第0次任务的第2次执行</span><br><span class="line">第0次任务的第3次执行</span><br><span class="line">第0次任务的第4次执行</span><br><span class="line">第1次任务的第0次执行</span><br><span class="line">第1次任务的第1次执行</span><br><span class="line">第1次任务的第2次执行</span><br><span class="line">第1次任务的第3次执行</span><br><span class="line">第1次任务的第4次执行</span><br><span class="line">第2次任务的第0次执行</span><br><span class="line">第2次任务的第1次执行</span><br><span class="line">第2次任务的第2次执行</span><br><span class="line">第2次任务的第3次执行</span><br><span class="line">第2次任务的第4次执行</span><br><span class="line">第3次任务的第0次执行</span><br><span class="line">第3次任务的第1次执行</span><br><span class="line">第3次任务的第2次执行</span><br><span class="line">第3次任务的第3次执行</span><br><span class="line">第3次任务的第4次执行</span><br><span class="line">第4次任务的第0次执行</span><br><span class="line">第4次任务的第1次执行</span><br><span class="line">第4次任务的第2次执行</span><br><span class="line">第4次任务的第3次执行</span><br><span class="line">第4次任务的第4次执行</span><br></pre></td></tr></table></figure>
<p>run(workStealingPool)未执行出结果，从Java 1.8开始存在</p>
</li>
</ol>
<p><a href="http://git.oschina.net/zaitingma/codes/2c4piong0klf6rxqemuds51" target="_blank" rel="noopener">示例代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Java/Java-常用加密算法(二)---对称加密(DES-3DES(TripleDES)-AES)/" rel="next" title="Java-常用加密算法(二)---对称加密(DES-3DES(TripleDES)-AES)">
                <i class="fa fa-chevron-left"></i> Java-常用加密算法(二)---对称加密(DES-3DES(TripleDES)-AES)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Java/Java-获取本机内外网ip/" rel="prev" title="Java-获取本机内外网ip">
                Java-获取本机内外网ip <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
